<?php
include('./fun.php');
$conn=my_sql($cfg);
$err['err']='ok';
$ac=g('ac');$cache_time=60;
$shop_id=g('shop_id');
$adm_id=g('adm_id');if(!$adm_id){err('参数错误');}
$redis=redis();//print_r($redis);
$adm=adm_login();
//$token=get_print_token();print_r($token);
switch($ac){
case 'print_add'://添加打印机
if(!$shop_id){err('请指定门店');}
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);if(!$data){err('数据错误');}
$arr['name']=$data['name'];if(!$arr['name']){err('请输入打印机名称');}
$arr['appid']=$data['appid'];if(!$arr['appid']){err('请输入打印机终端号');}
$arr['appkey']=$data['appkey'];if(!$arr['appkey']){err('请输入打印机密匙');}
$arr['adm_id']='0';
$arr['shop_id']=$shop_id;
$arr['date']=time();
$arr['c']='易联云';$arr['auto']=1;
//链接打印机
$token=get_print_token();
if(!$token){err('系统错误，请联系我们处理');}
unset($data);
$data['client_id']=$cfg['print_id'];
$data['machine_code']=$arr['appid'];
$data['msign']=$arr['appkey'];
$data['access_token']=$token['token'];
$data['sign']=$token['sign'];
$data['id']=$token['uuid'];
$data['timestamp']=$token['timestamp'];
$data['print_name']=$arr['name'];
$url='https://open-api.10ss.net/printer/addprinter';
$HTTP_headers='Content-Type:application/x-www-form-urlencoded';
$re=curlPost($url,$data,$HTTP_headers);
$re=json_decode($re,1);
if($re['error']==0){
insert($cfg['e'].'print',$arr);
err('ok');
}else{
err($re['error_description']);
}
break;

case 'print'://打印机文本
    $order_id=g('order_id');if(!$order_id){err('请选择订单');}
$id=g('id');if(!$id){err('请选择打印机');}
    $sql='select * from '.$cfg['e'].'order where id="'.a($order_id).'" ';
    $order=db1($sql);
    $order['date'] = date_ff($order['date']);
    if($order['yun'] == 'is_ps'){
        $order['yun'] = "配送";
    }elseif ($order['yun'] == 'is_zt'){
        $order['yun'] = "自提";
    }

$sql='select * from '.$cfg['e'].'print where id="'.a($id).'" and adm_id="'.$adm['id'].'" ';
$l=db1($sql);if(!$l){err('没有找到打印机');}
$data['client_id']=$cfg['print_id'];
$token=get_print_token();
if(!$token){err('系统错误，请联系我们处理');}
$data['access_token']=$token['token'];
$data['machine_code']=$l['appid'];
$data['content']=print_text($order);
$data['idempotence']=0;
$data['origin_id']=time();
$data['sign']=$token['sign'];
$data['id']=$token['uuid'];
$data['timestamp']=$token['timestamp'];
$url='https://open-api.10ss.net/print/index';
$HTTP_headers='Content-Type:application/x-www-form-urlencoded';
$re=curlPost($url,$data,$HTTP_headers);
$re=json_decode($re,1);
if($re['error']==0){
err('ok');
}else{
err($re['error_description']);
}
break;

case 'print_save':
$adm=adm_login();
$shop_id=g('shop_id');//查找门店
if(!$shop_id){err('请指定门店');}
if(!$adm){$user=has_login();}
$data=p('data');if(!$data){err('数据错误');}
$data=json_decode($data,1);
if(!$data or !is_array($data)){err('数据错误');}
$sql='select * from '.$cfg['e'].'shop where adm_id="'.$adm['id'].'" and id=:id';
$p['id']=$shop_id;
$l=db1($sql,$p);
if(!$l){err('没有找到门店或您没有保存的权限');}
$arr['print_data']=serialize($data);
update($cfg['e'].'shop',$arr,' where id="'.$l['id'].'"');
err('ok');
break;


case 'print_test'://测试打印
$adm=adm_login();
$shop_id=g('shop_id');//查找门店
if(!$shop_id){err('请指定门店');}
if(!$adm){$user=has_login();}

$sql='select * from '.$cfg['e'].'shop where adm_id="'.$adm['id'].'" and id=:id';
$p['id']=$shop_id;
$l=db1($sql,$p);
if(!$l){err('没有找到门店或者您没有管理的权限');}
if(!$l['print_data']){err('测试前，请先保存数据');}
$print_data=unserialize(out($l['print_data']));
//打印机
$sql='select * from '.$cfg['e'].'print where shop_id="'.$l['id'].'"';
$prints=db1($sql);
if(!$prints){err('抱歉 ，您的门店没有设置打印机');}

$re=print_format($print_data);
print_r($re);

$data['client_id']=$cfg['print_id'];
$token=get_print_token();
if(!$token){err('系统错误，请联系我们处理');}
$data['access_token']=$token['token'];
$data['machine_code']=$prints['appid'];
$data['content']=$re;
$data['idempotence']=0;
$data['origin_id']=time();
$data['sign']=$token['sign'];
$data['id']=$token['uuid'];
$data['timestamp']=$token['timestamp'];
$url='https://open-api.10ss.net/print/index';
$HTTP_headers='Content-Type:application/x-www-form-urlencoded';
$re=curlPost($url,$data,$HTTP_headers);
$re=json_decode($re,1);
if($re['error']==0){
err('ok');
}else{
err($re['error_description']);
}
break;


}


?>